package foodhelper.mtech.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import foodhelper.service.FoodHelperServiceFactory;
import foodhelper.service.IFoodHelperService;

public class BmiCaculator extends Activity {

	static final String BMI_DATA = "BmiData";
	private EditText m_heightET;
	private EditText m_weightET;
	private Button m_BmiBtn;
	private float bmi;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bmi_caculator);
		loadData();
		initControls();
	}
	public void loadData(){
		m_heightET = (EditText) findViewById(R.id.heightET);
		m_weightET = (EditText) findViewById(R.id.weightET);
		m_BmiBtn = (Button) findViewById(R.id.bmiBtn);
//		Log.d("BMI Send Data default", "" + mheight + mweight+ bmi);
	}
	public void initControls(){
		m_BmiBtn.setOnClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				try{
					String heightET = m_heightET.getText().toString();
					String weightET = m_weightET.getText().toString();
					if (heightET.isEmpty())
					{
						heightET="1";
					}
					
					if (weightET.isEmpty())
					{
						weightET="1";
					}
				Float mheight = Float.parseFloat(heightET)/100 ;
				Float mweight = Float.parseFloat(weightET) ;
				bmi =  Math.round((mweight)/(mheight*mheight));
				IFoodHelperService service = FoodHelperServiceFactory.getInstance().getService();
				try{
					service.WriteData("/sdcard/mysdfile.txt", String.valueOf(bmi));
				}catch(Exception ex){
					
				}
				Toast.makeText(getApplicationContext(), R.string.bmi_toast, Toast.LENGTH_SHORT).show();
				}catch(Exception e)
				{
					Log.d("BMI Send Data", e.toString()+ bmi);
					Toast.makeText(getApplicationContext(), R.string.toast1, Toast.LENGTH_SHORT).show();
					return;
				}
				Intent intent = new Intent(BmiCaculator.this,BmiShow.class);
				intent.putExtra(BMI_DATA, bmi);
				// TODO Auto-generated method stub
				startActivity(intent);	
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ibm_caculator, menu);
		return true;
	}

}
